<template>
  <div class="loginPanel">
    <h2>后台管理系统</h2>
    <el-tabs v-model="currentTab" type="border-card" stretch>
      <el-tab-pane name="account">
        <template #label>
          <span><i class="el-icon-date"></i> 账号登录</span>
        </template>
        <login-account ref="accountRef"></login-account>
      </el-tab-pane>
      <el-tab-pane name="phone">
        <template #label>
          <span><i class="el-icon-date"></i> 手机登录</span>
        </template>
        <login-phone ref="phoneRef"></login-phone>
      </el-tab-pane>
    </el-tabs>
    <div class="submit">
      <div class="top">
        <el-checkbox
          @change="handleChange"
          v-model="isKeepPaw"
          label="记住密码"
        ></el-checkbox>
        <a href="#">忘记密码</a>
      </div>
      <div class="bottom">
        <el-button type="primary" @click="login">立即登陆</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";
import LoginAccount from "./LoginAccount.vue";
import LoginPhone from "./LoginPhone.vue";
import storage from "@/utils/storage";
export default defineComponent({
  components: {
    LoginAccount,
    LoginPhone,
  },
  setup() {
    const isKeepPaw = ref(true);
    // 记录当前tab标签
    const currentTab = ref<string>("account");
    const accountRef = ref<InstanceType<typeof LoginAccount>>();
    const phoneRef = ref<InstanceType<typeof LoginPhone>>();
    const login = () => {
      // 由于上面ref没有赋值 可能为空 因此这里为可选链
      if (currentTab.value === "account") {
        // 账号登陆
        accountRef.value?.loginByAccount(isKeepPaw.value);
      } else {
        // 手机登陆
        phoneRef.value?.loginByPhone();
      }
    };
    const handleChange = (value: boolean) => {
      if (!value) {
        storage.removeStorage("username");
        storage.removeStorage("password");
      }
    };
    return {
      isKeepPaw,
      login,
      accountRef,
      phoneRef,
      handleChange,
      currentTab,
    };
  },
});
</script>

<style lang="less" scoped>
.loginPanel {
  width: 320px;
  display: inline-block;
  position: relative;
  top: 30%;
  left: 50%;
  transform: translateX(-50%);
  > h2 {
    margin-bottom: 20px;
    text-align: center;
    color: #666;
  }
  .submit {
    .top {
      display: flex;
      justify-content: space-between;
      a {
        color: #46a1f4;
        line-height: 39px;
      }
    }
    .bottom {
      .el-button {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
